﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Hydra.Shell.Implementations;
using MvvmTreeView;
using Hydra.Shell.Interfaces;
using Hydra.Shell.Constants;
using Hydra.Shell;
using Hydra.Shell.Enumerations;
using Utils;

namespace Hydra.Shell.Controls
{
    public partial class HydraUIHierarchyWindow : HydraDocViewControl, IHydraUIHierarchyWindow, IObjectWithSite
    {
        public HydraUIHierarchyWindow()
        {
            InitializeComponent();
        }

        public WindowsStyleTreeView TreeView
        {
            get
            {
                return HierarchyTreeView;
            }
        }

        public void SetSite(IUnknown pUnkSite)
        {
            var docView = this.DocView;
            var uiHierarchies = new List<IHydraUIHierarchy>();

            // we don't want to handle any commands until sited

            if (docView is ITreeView && docView is IHydraUIHierarchy)
            {
                var treeView = (ITreeView)docView;

                uiHierarchies.Add((IHydraUIHierarchy)docView);

                treeView.NodeDblClick += (sender2, e2) =>
                {
                    var node = (ITreeNode)sender2;
                    var args = (MouseButtonEventArgs)e2;

                    if (!args.Handled)
                    {
                        if (node is IHydraUIHierarchy)
                        {
                            uiHierarchies.Add((IHydraUIHierarchy)node);
                        }

                        foreach (var uiHierarchy in uiHierarchies)
                        {
                            var commands = new CommandSet<HydraUIHierarchyWindowCmdIds>();
                            var doubleClickCmdInfo = commands[(uint)HydraUIHierarchyWindowCmdIds.DoubleClick];

                            doubleClickCmdInfo.QueryCommandStatus = false;

                            if (uiHierarchy.GetCommands<HydraUIHierarchyWindowCmdIds>(commands))
                            {
                                if (doubleClickCmdInfo.Command != null && doubleClickCmdInfo.Command.CanExecute(node))
                                {
                                    doubleClickCmdInfo.Command.Execute(node);
                                }
                            }
                        }

                        args.Handled = true;
                    }
                };
            }
        }

        public int Init(IHydraUIHierarchy pUIH, Enumerations.UIHiearchyWindowFlags grfUIHWF, out IUnknown ppunkOutWindowFrame)
        {
            throw new NotImplementedException();
        }

        public int ExpandItem(IHydraUIHierarchy pUIH, uint itemid, Enumerations.ExpandFlags expf)
        {
            throw new NotImplementedException();
        }

        public int AddUIHierarchy(IHydraUIHierarchy pUIH, Enumerations.HydraAddHierarchyOptions grfAddOptions)
        {
            throw new NotImplementedException();
        }

        public int RemoveUIHierarchy(IHydraUIHierarchy pUIH)
        {
            throw new NotImplementedException();
        }

        public int SetWindowHelpTopic(string lpszHelpFile, uint dwContext)
        {
            throw new NotImplementedException();
        }

        public int GetItemState(IHydraUIHierarchy pHier, uint itemid, Enumerations.HydraHierarchyItemState dwStateMask, out Enumerations.HydraHierarchyItemState pdwState)
        {
            throw new NotImplementedException();
        }

        public int FindCommonSelectedHierarchy(uint grfOpt, out IHydraUIHierarchy lppCommonUIH)
        {
            throw new NotImplementedException();
        }

        public int SetCursor(IntPtr hNewCursor, out IntPtr phOldCursor)
        {
            throw new NotImplementedException();
        }

        public int GetCurrentSelection(out IntPtr ppHier, out uint pitemid, out IHydraMultiItemSelect ppMIS)
        {
            throw new NotImplementedException();
        }

        public bool GetSite(Guid riid, out IUnknown ppvSite)
        {
            throw new NotImplementedException();
        }

        public bool GetSite<T>(Guid riid, out T ppvSite)
        {
            throw new NotImplementedException();
        }
    }
}
